//
//  SelfAware.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/11/3.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit

public extension UIApplication {
    
    private static let runOnce: Void = {
        NothingToSeeHere.harmlessFunction()
    }()
    
    static func awake() {
        UIApplication.runOnce
    }
    
}

/// 自动执行方式 实现协议
public protocol SelfAware: AnyObject {
    static func awake()
}

public class NothingToSeeHere {

    static func harmlessFunction() {
        let typeCount = Int(objc_getClassList(nil, 0))
        let types = UnsafeMutablePointer<AnyClass>.allocate(capacity: typeCount)
        let safeTypes = AutoreleasingUnsafeMutablePointer<AnyClass>(types)
        objc_getClassList(safeTypes, Int32(typeCount))
        for index in 0 ..< typeCount { (types[index] as? SelfAware.Type)?.awake() }
        types.deinitialize(count: typeCount)
//        types.deallocate(capacity: typeCount)
    }

}
